﻿using System;
using System.Data;
using System.Security.Cryptography;
using System.Text;
using NIKE_Project.DAL;
using NIKE_Project.Model;

namespace NIKE_Project.BLL
{
    public class RegisterServer
    {
        private readonly DBHelper db = new DBHelper();
        /// <summary>
        /// 验证用户信息
        /// </summary>
        /// <param name="mobile"></param>
        /// <param name="pwd"></param>
        public DataSet ValidateUser(string mobile, string pwd)
        {
            if (pwd == "" || mobile == "")
                throw new Exception("用户名或密码不能为空");
            var sql = $"select SalesmanID,SalesmanName,Mobile,Gender,BaseSalary,CommissionRate,Role from Salesan WHERE Mobile='{mobile}' AND Pwd='{GenerateMD5(pwd)}'";
            var ds = db.getDataSet(sql);
            if (ds.Tables["nike"].Rows.Count == 0)
                throw new Exception("用户名或密码错误!");
            return ds;
        }

        /// <summary>
        /// 修改注册信息
        /// </summary>
        /// <param name="model"></param>
        public int RegisterUser(RegisterModel model)
        {
            //判断文本框是否有输入值，向数据库添加用户
            if (model.SalesmanName == "" || model.Mobile == "" || model.Pwd == "" || model.Role == "" || model.BaseSalary == "")
                throw new Exception("员工信息不能为空！");
            if (model.Role != "店长")
                throw new Exception("此注册只能为店长!");
            if (decimal.TryParse(model.CommissionRate, out var rate))
            {
                if (rate < 0 || rate > 1)
                    throw new Exception("提成比率必须在0~1之间!");
            }

            string sql;
            if (model.SalesmanID == "")
            {
                var sqlSearchMobile = $"SELECT SalesmanID FROM Salesan WHERE Mobile='{model.Mobile}'";
                var ds = db.getDataSet(sqlSearchMobile);
                if (ds.Tables["nike"].Rows.Count > 0)
                    throw new Exception("手机号已被注册，请重新注册！");
                //注册店长SQl
                sql = $@"INSERT INTO Salesan (SalesmanName,Mobile,Pwd,Gender,Role,BaseSalary,CommissionRate) 
                                VALUES('{model.SalesmanName}','{model.Mobile}','{GenerateMD5(model.Pwd)}',
                                                '{model.Gender}','{model.Role}','{model.BaseSalary}','{rate}')";
            }
            else
            {
                //修改店长SQl
                sql =
                    $@"update Salesan set 
                                                        SalesmanName='{model.SalesmanName}',
                                                        Mobile='{model.Mobile}',
                                                        Pwd='{GenerateMD5(model.Pwd)}',
                                                        Gender='{model.Gender}',
                                                        Role='{model.Role}',
                                                        BaseSalary='{model.BaseSalary}',
                                                        CommissionRate='{rate}' 
                                                        where Role='店长' And SalesmanID={model.SalesmanID}";
            }
            
           

            var rows = db.zsg(sql);
            if (rows <= 0)
            {
                throw new Exception("注册失败，请重新注册！");
            }
            return rows;
        }
        /// <summary>
        /// MD5加密
        /// </summary>
        /// <param name="txt"></param>
        /// <returns>加密后字符串</returns>
        public  string GenerateMD5(string txt)
        {
            var sb = new StringBuilder();
            using (var mi = MD5.Create())
            {
                var buffer = Encoding.Default.GetBytes(txt);
                //开始加密
                var newBuffer = mi.ComputeHash(buffer);
                foreach (var t in newBuffer)
                {
                    sb.Append(t.ToString("x2"));
                }
                return sb.ToString();
            }
        }
    }
}
